package org.opens.javaskill.thread.threadLocal;

import org.junit.Test;

/**
 * @Date 2020/9/16 20:34
 * @Created by 99126
 */
public class ThreadLocalDemo {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private static ThreadLocal<String> threadLocal2 = new ThreadLocal<String>() {

        /**
         * 说明:
         * <pre>
         *     1. 如果没有给ThreadLocal显示的设置值, 也就是执行器set方法, 则其get方法就会获取这个初始值.
         * </pre>
         */
        @Override
        protected String initialValue() {
            return "init";
        }

    };


    public static void main(String[] args) throws InterruptedException {
        threadLocal.set("alex1");
        Thread.sleep(1000);
        threadLocal.set("alex2");
        System.out.println(threadLocal.get());
    }

    /**
     * 功能:
     * <p>测试不给ThreadLocal设置值时, ThreadLocal的get方法会获取初始值(通过initialValue方法设置).</p>
     * 输出:
     * <pre>
     *     init
     * </pre>
     * 说明:
     * <pre>
     *      1. .
     * </pre>
     *
     * @date 2020/9/16 20:38
     * @auther zhangym
     */
    @Test
    public void test33() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println(threadLocal2.get());
    }

}